package persistence;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import persistence.exception.DAOException;
import persistence.util.JdbcUtil;

public class PredefinedParamDAOJdbcImpl extends JdbcDAOBase implements PredefinedParamDAO {
	
	public PredefinedParamDAOJdbcImpl() {
		super();
	}

	@Override
	public PredefinedParam findParamById(int id) throws DAOException {
		String sql = "SELECT id, parametername FROM auswahlparameter WHERE id = ?";
		Connection con = null;
		PreparedStatement prepStmt = null;
		ResultSet rs = null;
		PredefinedParam param = null;
		
		try {
			con = getConProvider().getConnection();
			prepStmt = con.prepareStatement(sql);
			prepStmt.setInt(1, id);
			rs = prepStmt.executeQuery();
			
			if(rs.next()) {
				param = new PredefinedParam(rs.getInt(1), rs.getString(2));
			}
		}
		catch (SQLException se) {
			throw new DAOException("Error when attempting to find PredefinedParam with id: "+ id, se);
		}
		finally {
			JdbcUtil.cleanup(con, prepStmt, rs);
		}
		return param;
	}

	@Override
	public PredefinedParam findParamByName(String name) throws DAOException {
		String sql = "SELECT id, parametername FROM auswahlparameter WHERE parametername = ?";
		Connection con = null;
		PreparedStatement prepStmt = null;
		ResultSet rs = null;
		PredefinedParam param = null;
		
		try {
			con = getConProvider().getConnection();
			prepStmt = con.prepareStatement(sql);
			prepStmt.setString(1, name);
			rs = prepStmt.executeQuery();
			
			if (rs.next())	
				param = new PredefinedParam(rs.getInt(1), rs.getString(2));
		}
		catch (SQLException se) {
			throw new DAOException("Problem when attempting to find Parameter with name: " + name, se);
		}
		finally {
			JdbcUtil.cleanup(con, prepStmt, rs);
		}
		return param;
	}

	@Override
	public List<PredefinedParam> findAllParams() throws DAOException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void updateParam(int id) throws DAOException {
		// TODO Auto-generated method stub

	}

	@Override
	public PredefinedParam create(int id, String name) throws DAOException {
		// TODO Auto-generated method stub
		return null;
	}

}
